from sqlalchemy import Column, Integer, String, Boolean, JSON, ForeignKey, UniqueConstraint
from sqlalchemy.orm import relationship
from api.database import Base

class TranslationConfig(Base):
    """
    翻译配置模型
    存储用户定义的多种翻译服务配置
    """
    __tablename__ = "translation_configs"
    
    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True)
    name = Column(String(100), nullable=False)  # 配置名称, e.g., "我的百度翻译"
    provider = Column(String(50), nullable=False)  # 'baidu', 'deeplx', 'ai'
    
    # 使用JSON字段存储不同服务商的凭证
    # baidu: {"appId": "...", "secretKey": "..."}
    # deeplx: {"url": "...", "key": "..."}
    # ai: {"url": "...", "key": "...", "model": "...", "prompt": "..."}
    credentials = Column(JSON, nullable=False)
    
    is_shared = Column(Boolean, default=False, nullable=False)  # 是否共享给其他用户
    
    # 关系
    user = relationship("User", back_populates="translation_configs")
    
    __table_args__ = (
        UniqueConstraint('user_id', 'name', name='_user_id_name_uc'),
    )